package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.cost.CostService;
import com.xcf.service.business.common.customs.CustomsService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/cost")
public class CostController extends BaseController {
	@Resource
	private CostService soctservice;

	String menuUrl = "cost/management"; // 菜单地址(权限用)

	@RequestMapping(value = "/management")
	//条件查询及列表查询
	public ModelAndView cost(Page page){	
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();	
			mv.setViewName("/business/common/cost/costmanagment");	
		return mv;
		
	}
	@RequestMapping(value="/list")
	public void list(HttpServletResponse response) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>list=soctservice.seach(pd);
		JSONArray array = new JSONArray();
		for (PageData cost : list) {
			JSONObject ob = new JSONObject();
			ob.put("ID", cost.get("ID"));
			ob.put("FAST_SEARCH_CODE", cost.getString("FAST_SEARCH_CODE"));
			ob.put("FEE_NAME", cost.getString("FEE_NAME"));
			if(cost.get("ENABLE").toString().equals("0")||cost.get("ENABLE")==null){
				String enable="否";
				ob.put("ENABLE", enable);
			}else{
				String enable="是";
				ob.put("ENABLE", enable);
			}
			
			ob.put("DEPARTMENT", cost.getString("DEPARTMENT"));
			
			if(cost.get("IS_ACCEPT_AMEND")==null||cost.get("IS_ACCEPT_AMEND").toString().equals("0")){
				String enable="否";
				ob.put("IS_ACCEPT_AMEND", enable);
			}else{
				String enable="是";
				ob.put("IS_ACCEPT_AMEND", enable);
			}
			
			if(cost.get("IS_SPECIAL_FEE")==null||cost.get("IS_SPECIAL_FEE").toString().equals("0")){
				String enable="否";
				ob.put("IS_SPECIAL_FEE", enable);
			}else{
				String enable="是";
				ob.put("IS_SPECIAL_FEE", enable);
			} 
			ob.put("OPEN_TICKET_FEE_NAME", cost.getString("OPEN_TICKET_FEE_NAME"));//开票费用名称
			ob.put("OPEN_TICKET_TAX_RATE", cost.get("OPEN_TICKET_TAX_RATE"));//开票税率
			ob.put("REMARK", cost.getString("REMARK"));
			ob.put("FEE_CODE", cost.getString("FEE_CODE"));
			array.add(ob);
		}
		response.setContentType("text/html; charset=utf-8");         
	    response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows"+"\""+":"+array+"}");
	}
	//开票费用名称下拉
	@RequestMapping(value="/feiyong")
	public void feiyong(HttpServletResponse response) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData> list =soctservice.ser(pd);
		JSONArray array= new JSONArray();
		for (PageData data : list) {
			JSONObject ob = new JSONObject();
			ob.put("id", data.get("id"));
			ob.put("fee_name",data.getString("fee_name"));
			ob.put("tax_rate",data.get("tax_rate"));
			array.add(ob);
		}
		response.setContentType("text/html; charset=utf-8");         
	    response.getWriter().write("{"+"\""+"total2"+"\""+":"+"\""+"5"+"\""+","+"\""+"rows2"+"\""+":"+array+"}");
		
	}
//	@RequestMapping(value="/fename")
//	public void fename(HttpServletResponse response) throws Exception{
//		PageData pd = new PageData();
//		pd = this.getPageData();
//		PageData list =soctservice.sera(pd);
//		JSONObject ob = new JSONObject();
//		ob.put("id", list.get("id"));
//		ob.put("fee_name",list.getString("fee_name"));
//		ob.put("tax_rate", list.get("tax_rate"));
//		
//		
//		response.setContentType("text/html; charset=utf-8");         
//	    response.getWriter().write("{"+"\""+"total2"+"\""+":"+"\""+"5"+"\""+","+"\""+"rows2"+"\""+":"+ob+"}");	
//		
//	}

	//保存及修改
		@RequestMapping(value="/addcost")
		@ResponseBody
		public String addcustoms() throws Exception {				
			PageData pd = new PageData();
			    pd = this.getPageData();
				
			    if(pd.getString("ID").isEmpty()){
			       
			        soctservice.addcost(pd);
			    }else{
			    	if(pd.get("ENABLE").equals("是")){
			    		int a=1;
			    		pd.put("ENABLE", a);
			    	}else{
			    		int a=0;
			    		pd.put("ENABLE", a);
			    	}
			    	if(pd.get("IS_ACCEPT_AMEND").equals("是")){
			    		int a=1;
			    		pd.put("IS_ACCEPT_AMEND", a);
			    	}else{
			    		int a=0;
			    		pd.put("IS_ACCEPT_AMEND", a);
			    	}
			    	if(pd.get("IS_SPECIAL_FEE").equals("是")){
			    		int a=1;
			    		pd.put("IS_SPECIAL_FEE", a);
			    	}else{
			    		int a=0;
			    		pd.put("IS_SPECIAL_FEE", a);
			    	}
			    	
			    	soctservice.savaU(pd);
			    }
			  
				return null;
		}
	
	//删除
	@RequestMapping(value="deleteU")
	@ResponseBody
	
		public String deleteinfo() throws Exception{
			PageData pd = new PageData();
			pd = this.getPageData();
			
					
					String data = pd.getString("array");
					
					JSONArray items = JSONArray.fromObject(data);
					for(int i=0,size=items.size();i<size;i++){
						JSONObject item = items.getJSONObject(i);
						String id = item.getString("id").trim();
						PageData pdq = new PageData();
						pdq.put("ID", id);
						soctservice.delete(pdq);
				}
					return null;
					
		}
}
